// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.generators;

import java.util.HashMap;
import java.util.Map;

import org.dengues.core.property.EParameterFieldCategory;
import org.dengues.core.property.IPropertyController;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.dengues.designer.ui.properties.controllers.AbstractPropertyController;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-1 上午11:22:03
 */
public class DynamicPropertyGenerators {

    private static DynamicPropertyGenerators instance = null;

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getInstance".
     * 
     * @return
     */
    public static DynamicPropertyGenerators getInstance() {
        if (instance == null) {
            instance = new DynamicPropertyGenerators();
        }
        return instance;
    }

    private Map<EParameterFieldCategory, AbstractPropertyController> controllers;

    public void initController(PropertyTabDynamicSection dps) {
        controllers = new HashMap<EParameterFieldCategory, AbstractPropertyController>();
        IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
                IPropertyController.EXTENSION_ID);
        for (IConfigurationElement element : elements) {
            String field = element.getAttribute(IPropertyController.EXTENSION_FIELD);
            EParameterFieldCategory category = EParameterFieldCategory.getCategoryFromField(field);
            if (!controllers.containsKey(category)) {
                try {
                    AbstractControllerGenerator extension = (AbstractControllerGenerator) element
                            .createExecutableExtension(IPropertyController.EXTENSION_CLASS);
                    extension.setDynamicPropertySection(dps);
                    AbstractPropertyController generate = extension.generate();
                    controllers.put(category, generate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public AbstractPropertyController getController(EParameterFieldCategory category) {
        if (controllers.containsKey(category)) {
            AbstractPropertyController controller = controllers.get(category);
            if (controller != null) {
                controller.initialize();
                return controller;
            } else {
                controllers.remove(category);
            }
        }
        return null;
    }
}
